package com;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class FileCopy extends JFrame implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField[] tf=new JTextField[2];
	private JButton[] bt=new JButton[2];
	private JButton copyBt;
	private JProgressBar pb;
	private File srcFile;
	
	public FileCopy() {
		setSize(400, 500);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLayout(null);
		
		String[] str={"源文件：","目的位置："};
		for(int i=0;i<str.length;i++){
			JLabel label=new JLabel(str[i]);
			label.setSize(100, 25);
			label.setLocation(30, 50+i*40);
//			label.setBounds(30, 50+i*40, 100, 50);
			add(label);
			tf[i]=new JTextField(10);
			tf[i].setSize(200, 25);
			tf[i].setLocation(100,50+i*40);
			add(tf[i]);
			bt[i]=new JButton("...");
			bt[i].setSize(50, 25);
			bt[i].setLocation(310, 50+i*40);
			bt[i].addActionListener(this);
			add(bt[i]);
		}
		
		pb=new JProgressBar(0, 100);
		pb.setSize(300, 25);
		pb.setLocation(40, 200);
		pb.setStringPainted(true);
		pb.setValue(30);
		add(pb);
		
		copyBt=new JButton("复制");
		copyBt.setBounds(100, 280, 100, 25);
		copyBt.addActionListener(this);
		add(copyBt);
	}
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				new FileCopy();
			}
		});
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		Object obj=e.getSource();
		if(obj==bt[0]){
			JFileChooser fc=new JFileChooser();
			int f=fc.showOpenDialog(this);
			if(f==JFileChooser.APPROVE_OPTION){
				srcFile=fc.getSelectedFile();
				tf[0].setText(srcFile.getPath());
			}
		}
		else if(obj==bt[1]){
			JFileChooser fc=new JFileChooser();
			fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			int f=fc.showOpenDialog(this);
			if(f==JFileChooser.APPROVE_OPTION){				
				tf[1].setText(fc.getSelectedFile().getPath());
			}
		}
		else if(obj==copyBt){
			File desFile=new File(tf[1].getText()+"\\"+srcFile.getName());
			System.out.println(srcFile.getPath());
			System.out.println(desFile.getPath());
			copyFile(srcFile,desFile);
		}
	}
	private void copyFile(File srcFile, File desFile) {
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				try {
					BufferedInputStream bis=new BufferedInputStream(new FileInputStream(srcFile));
					BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(desFile));
					int a=bis.read(),c=0;
					long len=srcFile.length();
					int percent;
					while(a!=-1){
						bos.write(a);
						c++;
						percent=(int)(1.0*c/len*100);
						pb.setValue(percent);
						a=bis.read();
					}
					bis.close();bos.flush();bos.close();
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}).start();
	}

}
